package com.school.movie.business.web.controller;

import com.school.movie.business.web.model.vo.*;
import com.school.movie.common.utils.BaseConstants;
import com.school.movie.common.utils.ContextUtils;
import com.school.movie.common.bean.PageBean;
import com.school.movie.service.imp.PlanServiceImp;
import com.school.movie.service.imp.SysLogServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/plan")
public class PlanController {

    @Inject
    PlanServiceImp planService;
    @Autowired
    SysLogServiceImp logService;
    @Value("${upload.absPath}")
    String absPath;

    @RequestMapping("/list")
    public String list(PlanVo plan, PageBean pager, HttpSession session, ModelMap map) {

        if (plan == null || plan.getStartTime() == null) {
            if (session.getAttribute("plan") != null) {
                plan = (PlanVo) session.getAttribute("plan");
            }
        } else {
            session.setAttribute("plan", plan);
        }
        getParams(plan, pager, map);
        return "plan";
    }

    @PostMapping("/save")
    public String save(HttpSession session, PlanVo plan, ModelMap map) {
        Manager mgr = ContextUtils.getManagerInfo();
//        if(mgr.getManagerType()!=3){
        plan.setAgentId(mgr.getAgentId());
//        }
        if (plan.getPlanId() > 0) {
            planService.update(plan);
            logService.add(BaseConstants.LOG_UPDATE, plan.toString());
        } else {
            planService.add(plan);
            logService.add(BaseConstants.LOG_ADD, plan.toString());
        }
        plan = new PlanVo();
        if (session.getAttribute("plan") != null) {
            plan = (PlanVo) session.getAttribute("plan");
        }
        map = getParams(plan, new PageBean(), map);
        return "plan";
    }

    private ModelMap getParams(PlanVo plan, PageBean pager, ModelMap map) {
        Manager mgr = ContextUtils.getManagerInfo();
//        if(mgr.getManagerType()!=3){
        plan.setAgentId(mgr.getAgentId());
//        }
        int cnt = planService.count(plan);
        pager.setTotalRows(cnt);
        List<PlanVo> planList = planService.list(plan, pager);
        pager.setData(planList);
        List<MovieVo> movieList = planService.movieList();
        List<MovieHallVo> hallList = planService.hallList(plan.getAgentId());
//        List<AgentInfo> agentList = planService.agentList(plan.getAgentId());
        List<PlayTimeVo> playTimeList = planService.playTimeList();
        map.addAttribute("pager", pager);
        map.addAttribute("plan", plan);
        map.addAttribute("movieList", movieList);
        map.addAttribute("hallList", hallList);
//        map.addAttribute("agentList",agentList);
        map.addAttribute("playTimeList", playTimeList);
        return map;
    }

    @RequestMapping("/add")
    public String add(ModelMap map) {
        PlanVo plan = new PlanVo();
        Manager mgr = ContextUtils.getManagerInfo();
//        if(mgr.getManagerType()!=3){
        plan.setAgentId(mgr.getAgentId());
//        }
        List<MovieVo> movieList = planService.movieList();
        List<MovieHallVo> hallList = planService.hallList(plan.getAgentId());
//        List<AgentInfo> agentList = planService.agentList(plan.getAgentId());
        List<PlayTimeVo> playTimeList = planService.playTimeList();
        map.addAttribute("plan", plan);
        map.addAttribute("movieList", movieList);
        map.addAttribute("hallList", hallList);
//        map.addAttribute("agentList",agentList);
        map.addAttribute("playTimeList", playTimeList);
        return "planAdd";
    }

    @RequestMapping("/update")
    public String update(PlanVo plan, ModelMap map) {
        plan = planService.getById(plan.getPlanId());

        Manager mgr = ContextUtils.getManagerInfo();
        int agentId = 0;
//        if(mgr.getManagerType()!=3){
        agentId = mgr.getAgentId();
//        }
        List<MovieVo> movieList = planService.movieList();
        List<MovieHallVo> hallList = planService.hallList(plan.getAgentId());
//        List<AgentInfo> agentList = planService.agentList(agentId);
        List<PlayTimeVo> playTimeList = planService.playTimeList();
        map.addAttribute("plan", plan);
        map.addAttribute("movieList", movieList);
        map.addAttribute("hallList", hallList);
//        map.addAttribute("agentList",agentList);
        map.addAttribute("playTimeList", playTimeList);

        return "planAdd";
    }

    @RequestMapping("/del")
    public String del(String ids, ModelMap map) {
        String[] arr = ids.split(",");
        planService.del(arr);
        getParams(new PlanVo(), new PageBean(), map);
        return "plan";
    }
}
